package com.dotmedia.dboperations;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dotmedia.core.DatabaseAccessService;
import com.dotmedia.entities.Ad;

/**
 * This class deals with the database operations for the Ad entity
 * 
 * @author siddharthsaha
 * 
 */
public class AdDatabaseOperations {

	private static Logger logger = Logger.getLogger("dboperations");

	public void persistAdEntity(Ad adEntity) {
		logger.info("IN method peristAdEntity() in AdDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int persistStatus = databaseAccessService.persistEntity(adEntity);
		if (persistStatus == 0) {
			logger.info("The Ad entity was successfully persisted");
		} else {
			logger.error("The Ad entity was not successfully persisted in the database");
		}
		logger.info("OUT method persistAdEntity() in AdDatabaseOperations");
	}

	public void removeAdEntity(Ad adEntity) {
		logger.info("IN method removeAdEntity() in AdDatabaseOperations");
		logger.info("Removing Ad entity with id: " + adEntity.getAdId());
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int removeStatus = databaseAccessService.removeEntity(adEntity, adEntity.getAdId().intValue());
		if (removeStatus == 0) {
			logger.info("The Ad entity was successfully removed from the database");
		} else {
			logger.error("The Ad entity was not successfully removed from the database");
		}
		logger.info("OUT method removeAdEntity() in AdDatabaseOperations");
	}

	public void updateAdEntity(Ad adEntity) {
		logger.info("IN method updateAdEntity() in AdDatabaseOperations");
		String query = "SELECT adEntity FROM Ad adEntity WHERE adEntity.adRefCode = '" + adEntity.getAdRefCode() + "' AND adEntity.system.id = "
				+ adEntity.getSystem().getId().intValue();
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrivedEntities = databaseAccessService.getEntities(query);
		boolean updated = false;
		for (int index = 0; index < retrivedEntities.size(); index++) {
			Ad currentAdEntity = (Ad) retrivedEntities.get(index);
			if (currentAdEntity.getSystem().getId().intValue() == adEntity.getSystem().getId().intValue()
					&& currentAdEntity.getAdRefCode().equals(adEntity.getAdRefCode())) {
				// got the entity which needs to be updated
				logger.info("Obtained the Ad entity which needs to be updated");
				currentAdEntity.setAdsize(adEntity.getAdsize());
				currentAdEntity.setAdTemplate(adEntity.getAdTemplate());
				currentAdEntity.setAdTplCode(adEntity.getAdTplCode());
				currentAdEntity.setAdvertiser(adEntity.getAdvertiser());
				currentAdEntity.setCreatedTimestamp(adEntity.getCreatedTimestamp());
				currentAdEntity.setDescription(adEntity.getDescription());
				currentAdEntity.setIsModified(adEntity.getIsModified());
				currentAdEntity.setLastEditBy(adEntity.getLastEditBy());
				currentAdEntity.setLastEditTimestamp(adEntity.getLastEditTimestamp());
				currentAdEntity.setStatus(adEntity.getStatus());
				int updateStatus = databaseAccessService.updateEntity(currentAdEntity);
				if (updateStatus == 0) {
					updated = true;
				} else {
					updated = false;
				}
				break;
			}
		}
		if (updated) {
			logger.info("The Ad entity was successfully updated");
		} else {
			logger.error("The Ad entity was not successfully updated");
		}
		logger.info("OUT method updateAdEntity() in AdDatabaseOperations");
	}

	public List<Ad> getAllAdEntities() {
		logger.info("IN method getAllAdEntities() in AdDatabaseOperations");
		List<Ad> toReturnList = new ArrayList<Ad>();
		String query = "SELECT adEntity FROM Ad adEntity";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retreivedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retreivedEntities.size(); index++) {
			Ad currentAdEntity = (Ad) retreivedEntities.get(index);
			toReturnList.add(currentAdEntity);
		}
		logger.info("OUT method getAllAdEntities() in AdDatabaseOperations");
		return toReturnList;
	}

	public List<Ad> getAdEntityByAdId(int adId) {
		logger.info("IN method getAdEntityByAdId() in AdDatabaseOperations");
		List<Ad> toReturnList = new ArrayList<Ad>();
		String query = "SELECT adEntity FROM Ad adEntity WHERE adEntity.adId = " + adId;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retreivedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retreivedEntities.size(); index++) {
			Ad currentAdEntity = (Ad) retreivedEntities.get(index);
			if (currentAdEntity.getAdId().intValue() == adId) {
				toReturnList.add(currentAdEntity);
			}
		}
		logger.info("OUT method getAdEntityByAdId() in AdDatabaseOperations");
		return toReturnList;
	}

	public List<Ad> getAdEntitiesByAdRefCode(String adRefCode) {
		logger.info("IN method getAdEntitiesByAdRefCode() in AdDatabaseOperations");
		List<Ad> toReturnList = new ArrayList<Ad>();
		String query = "SELECT adEntity FROM Ad adEntity WHERE adEntity.adRefCode = '" + adRefCode + "'";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retreivedList = databaseAccessService.getEntities(query);
		for (int index = 0; index < retreivedList.size(); index++) {
			Ad currentAdEntity = (Ad) retreivedList.get(index);
			if (currentAdEntity.getAdRefCode().equals(adRefCode)) {
				toReturnList.add(currentAdEntity);
			}
		}
		logger.info("OUT method getAdEntitiesByAdRefCode() in AdDatabaseOperations");
		return toReturnList;
	}

	public List<Ad> getAdEntitiesByAdvertiserId(int advertiserId) {
		logger.info("IN method getAdEntitiesByAdvertiserId() in AdDatabaseOperations");
		List<Ad> toReturnList = new ArrayList<Ad>();
		String query = "SELECT adEntity FROM Ad adEntity WHERE adEntity.advertiser.advertiserId = " + advertiserId;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrivedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrivedEntities.size(); index++) {
			Ad currentAdEntity = (Ad) retrivedEntities.get(index);
			if (currentAdEntity.getAdvertiser().getAdvertiserId().intValue() == advertiserId) {
				toReturnList.add(currentAdEntity);
			}
		}
		logger.info("OUT method getAdEntitiesByAdvertiserId() in AdDatabaseOperations");
		return toReturnList;
	}

	public List<Ad> getAdEntitiesByAdsizeId(int adsizeId) {
		logger.info("IN method getAdEntitiesByAdsizeId() in AdDatabaseOperations");
		List<Ad> toReturnList = new ArrayList<Ad>();
		String query = "SELECT adEntity FROM Ad adEntity WHERE adEntity.adsize.adsizeId = " + adsizeId;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedEntities.size(); index++) {
			Ad currentAdEntity = (Ad) retrievedEntities.get(index);
			if (currentAdEntity.getAdsize().getAdsizeId().intValue() == adsizeId) {
				toReturnList.add(currentAdEntity);
			}
		}
		logger.info("OUT method getAdEntitiesByAdsizeId() in AdDatabaseOperations");
		return toReturnList;
	}

	public List<Ad> getAdEntitiesByAdTplId(int adTplId) {
		logger.info("IN method getAdEntitiesByAdtplId() in AdDatabaseOperations");
		List<Ad> toReturnList = new ArrayList<Ad>();
		String query = "SELECT adEntity FROM Ad adEntity WHERE adEntity.adTemplate.adtplId = " + adTplId;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retreivedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retreivedEntities.size(); index++) {
			Ad currentAdEntity = (Ad) retreivedEntities.get(index);
			if (currentAdEntity.getAdTemplate().getAdtplId().intValue() == adTplId) {
				toReturnList.add(currentAdEntity);
			}
		}
		logger.info("OUT method getAdEntitiesByAdTplId() in AdDatabaseOperations");
		return toReturnList;
	}

	public List<Ad> getModifiedAds() {
		logger.info("IN method getModifiedAds() in AdDatabaseOperations");
		List<Ad> toReturnList = new ArrayList<Ad>();
		String query = "SELECT adEntity FROM Ad adEntity WHERE adEntity.isModified = " + true;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrivedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrivedEntities.size(); index++) {
			Ad currentAdEntity = (Ad) retrivedEntities.get(index);
			if (currentAdEntity.getIsModified().booleanValue() == true) {
				toReturnList.add(currentAdEntity);
			}
		}
		logger.info("OUT method getModifiedAds() in AdDatabaseOperations");
		return toReturnList;
	}

	public List<Ad> getNotModifiedAds() {
		logger.info("IN method getNotModifiedAds() in AdDatabaseOperations");
		List<Ad> toReturnList = new ArrayList<Ad>();
		String query = "SELECT adEntity FROM adEntity WHERE adEntity.isModified = " + false;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedEntities.size(); index++) {
			Ad currentAdEntity = (Ad) retrievedEntities.get(index);
			if (currentAdEntity.getIsModified().booleanValue() == false) {
				toReturnList.add(currentAdEntity);
			}
		}
		logger.info("OUT method getNotModifiedAds() in AdDatabaseOperatons");
		return toReturnList;
	}

}
